
"use strict";

(function (e) {
  var r = function () {},
    l = function () {
      this.option = {
        fontsize: 20, //文字大小
        color: "#ffffff", // 字体颜色
        backgroundColor: "#ccc", //背景色
        width: 30, //宽度
        height: 30, //高度
        lineHeight: 30, //行高
        bottom: 60, //底部距离
        right: 10, //右边距离
        scrollTop: 500, //滚动距离
        html: "", //html
      };
      this._op = {
        index: 0,
      };
    };
  //初始化
  r.prototype.init = l;
  //打开
  l.prototype.open = function (option) {
    var obj=this._opt = Object.assign({}, this.option, option);
    
    if(this._opt.html==""){
    $("body").append("<div name='lanui-scrolltop' title='回到顶部'"+
        "style='background-color:"+this._opt.backgroundColor+
        ";font-size:"+this._opt.fontsize+
        "px;color:"+this._opt.color+
        ";width:"+this._opt.width+
        "px;height:"+this._opt.height+
        "px;line-height:"+this._opt.lineHeight+
        "px;bottom:"+this._opt.bottom+
        "px;right:"+this._opt.right+
        "px'></div>");
    }else{
        $("body").append("<div class='lanui-scrolltop' name='lanui-scrolltop' title='回到顶部'"+
        "style='background-color:"+this._opt.backgroundColor+
        ";font-size:"+this._opt.fontsize+
        "px;color:"+this._opt.color+
        ";width:"+this._opt.width+
        "px;height:"+this._opt.height+
        "px;line-height:"+this._opt.lineHeight+
        "px;bottom:"+this._opt.bottom+
        "px;right:"+this._opt.right+
        "px'>"+this._opt.html+"</div>");
    };

    //top事件
    $(document).on({
        click: function() {
            let t = setInterval(function() {
                if ($(document).scrollTop() > 0) {
                    $(document).scrollTop($(document).scrollTop() / 1.1)
                } else {
                    clearInterval(t);
                }
            }, 10)
        }
    }, "div[name=lanui-scrolltop]");

    //文档滚动
    $(document).on({
        scroll: function() {
            scrollTop(obj);
        }
    });

    //判断是否显示回到顶部
    function scrollTop(obj) {
        if ((document.documentElement.scrollTop || document.body.scrollTop) > obj.scrollTop) {
            $("div[name=lanui-scrolltop]").css("display", "block");
        } else {
            $("div[name=lanui-scrolltop]").css("display", "none");
        }
    };
  };
  
  e.scrollTop = new r();
})(window);